//
//  DSPtabBar.swift
//  SWweibo
//
//  Created by 杜守鹏 on 15/12/26.
//  Copyright © 2015年 杜守鹏. All rights reserved.
//

import UIKit

class DSPtabBar: UITabBar {
    
    var didClickPlusButton:(() ->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(plusButton)
        backgroundImage = UIImage(named: "tabbar_background")
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    private lazy var plusButton:UIButton = {
       let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Highlighted)
        
         button.addTarget(self, action: "didClickButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.sizeToFit()
        
       
  
      return button
    }()
    
  @objc private  func didClickButton(){
        
        didClickPlusButton?()
    }
 
    override func layoutSubviews() {
        super.layoutSubviews()
        plusButton.center.x = bounds.size.width * 0.5
        plusButton.center.y = bounds.size.height * 0.5
        let childW = bounds.size.width * 0.2
        
        var index = 0
        
        for value in subviews{
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                value.frame.size.width = childW
                value.frame.origin.x = childW * CGFloat(index)
                
                if index == 1{
                    index++
                }
                index++
            }
        }
    }
}
